<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Validation</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="filter.examples.html">范例</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="filter.examples.sanitization.html">Sanitization</a></div>
 <div class="up"><a href="filter.examples.html">范例</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="filter.examples.validation" class="section">
  <h2 class="title">Validation</h2>
  <p class="para">
   <div class="example" id="example-4988">
    <p><strong>Example #1 Validating email addresses with <span class="function"><a href="function.filter-var.html" class="function">filter_var()</a></span></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$email_a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'joe@example.com'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$email_b&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'bogus'</span><span style="color: #007700">;<br /><br />if&nbsp;(</span><span style="color: #0000BB">filter_var</span><span style="color: #007700">(</span><span style="color: #0000BB">$email_a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">FILTER_VALIDATE_EMAIL</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"This&nbsp;(</span><span style="color: #0000BB">$email_a</span><span style="color: #DD0000">)&nbsp;email&nbsp;address&nbsp;is&nbsp;considered&nbsp;valid."</span><span style="color: #007700">;<br />}<br />if&nbsp;(</span><span style="color: #0000BB">filter_var</span><span style="color: #007700">(</span><span style="color: #0000BB">$email_b</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">FILTER_VALIDATE_EMAIL</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"This&nbsp;(</span><span style="color: #0000BB">$email_b</span><span style="color: #DD0000">)&nbsp;email&nbsp;address&nbsp;is&nbsp;considered&nbsp;valid."</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
This (joe@example.com) email address is considered valid.
</pre></div>
    </div>
   </div>
  </p>

  <p class="para">
   <div class="example" id="example-4989">
    <p><strong>Example #2 Validating IP addresses with <span class="function"><a href="function.filter-var.html" class="function">filter_var()</a></span></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$ip_a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'127.0.0.1'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$ip_b&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'42.42'</span><span style="color: #007700">;<br /><br />if&nbsp;(</span><span style="color: #0000BB">filter_var</span><span style="color: #007700">(</span><span style="color: #0000BB">$ip_a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">FILTER_VALIDATE_IP</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"This&nbsp;(ip_a)&nbsp;IP&nbsp;address&nbsp;is&nbsp;considered&nbsp;valid."</span><span style="color: #007700">;<br />}<br />if&nbsp;(</span><span style="color: #0000BB">filter_var</span><span style="color: #007700">(</span><span style="color: #0000BB">$ip_b</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">FILTER_VALIDATE_IP</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"This&nbsp;(ip_b)&nbsp;IP&nbsp;address&nbsp;is&nbsp;considered&nbsp;valid."</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
This (ip_a) IP address is considered valid.
</pre></div>
    </div>
   </div>
  </p>

  <p class="para">
   <div class="example" id="example-4990">
    <p><strong>Example #3 Passing options to <span class="function"><a href="function.filter-var.html" class="function">filter_var()</a></span></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$int_a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'1'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$int_b&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'-1'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$int_c&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'4'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$options&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'options'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'min_range'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'max_range'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)<br />);<br />if&nbsp;(</span><span style="color: #0000BB">filter_var</span><span style="color: #007700">(</span><span style="color: #0000BB">$int_a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">FILTER_VALIDATE_INT</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$options</span><span style="color: #007700">)&nbsp;!==&nbsp;</span><span style="color: #0000BB">FALSE</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"This&nbsp;(int_a)&nbsp;integer&nbsp;is&nbsp;considered&nbsp;valid&nbsp;(between&nbsp;0&nbsp;and&nbsp;3).\n"</span><span style="color: #007700">;<br />}<br />if&nbsp;(</span><span style="color: #0000BB">filter_var</span><span style="color: #007700">(</span><span style="color: #0000BB">$int_b</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">FILTER_VALIDATE_INT</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$options</span><span style="color: #007700">)&nbsp;!==&nbsp;</span><span style="color: #0000BB">FALSE</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"This&nbsp;(int_b)&nbsp;integer&nbsp;is&nbsp;considered&nbsp;valid&nbsp;(between&nbsp;0&nbsp;and&nbsp;3).\n"</span><span style="color: #007700">;<br />}<br />if&nbsp;(</span><span style="color: #0000BB">filter_var</span><span style="color: #007700">(</span><span style="color: #0000BB">$int_c</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">FILTER_VALIDATE_INT</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$options</span><span style="color: #007700">)&nbsp;!==&nbsp;</span><span style="color: #0000BB">FALSE</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"This&nbsp;(int_c)&nbsp;integer&nbsp;is&nbsp;considered&nbsp;valid&nbsp;(between&nbsp;0&nbsp;and&nbsp;3).\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$options</span><span style="color: #007700">[</span><span style="color: #DD0000">'options'</span><span style="color: #007700">][</span><span style="color: #DD0000">'default'</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />if&nbsp;((</span><span style="color: #0000BB">$int_c&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">filter_var</span><span style="color: #007700">(</span><span style="color: #0000BB">$int_c</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">FILTER_VALIDATE_INT</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$options</span><span style="color: #007700">))&nbsp;!==&nbsp;</span><span style="color: #0000BB">FALSE</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"This&nbsp;(int_c)&nbsp;integer&nbsp;is&nbsp;considered&nbsp;valid&nbsp;(between&nbsp;0&nbsp;and&nbsp;3)&nbsp;and&nbsp;is&nbsp;</span><span style="color: #0000BB">$int_c</span><span style="color: #DD0000">."</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
This (int_a) integer is considered valid (between 0 and 3).
This (int_c) integer is considered valid (between 0 and 3) and is 1.
</pre></div>
    </div>
   </div>
  </p>
 </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="filter.examples.html">范例</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="filter.examples.sanitization.html">Sanitization</a></div>
 <div class="up"><a href="filter.examples.html">范例</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
